Return to doc.sitecore.com

Valid for Sitecore 5.2, 5.1.1
  GetTemplates
Prev Next

GetTemplates returns the collection of templates that define the structure of all items stored by the data source.  If the data provider does not contain any templates, the method should return null.

TemplateCollection GetTemplates(
  CallContext context
)

Sitecore calls GetTemplates as it initializes the data source.  The method must return all the template definitions the data source contains.  The Template class defines a single template.  The TemplateCollection class is simple a list of Template classes.

Template classes can be built using the helper class Template.Builder, or template definitions can be parsed from an xml file using the Sitecore.Data.Templates.TemplateParser class.  The following code from the Hello World example illustrates how to use the Template.Builder class.

public override TemplateCollection GetTemplates(CallContext context)
{
  Template[] templates = new Template[1];

  // You can generate unique GUIDs in MS SQL Server via "select newid()"
  ID templateID = new ID("{ABD9F13F-DFAC-4ED9-8C23-E0A9C5BB19C9}");

  // Alternatively, you can create new IDs using the ID class
  ID sectionID = ID.NewID; //new ID("{AAC6F139-EC27-4C88-A8A2-ABEBCAF53117}");
  ID fieldID = new ID("{3F3FF111-1EE3-4181-A7E8-DFC489EAB2C4}");

  // We need to return a template collection, and we
  // pass it as the owner of all templates in the template builder
  TemplateCollection tcollection = new TemplateCollection();

  // Create the template
  Template.Builder tbuilder = new Template.Builder("Hello World Template",
                                                   templateID, tcollection);
  tbuilder.SetFullName("Hello World Template");
  tbuilder.SetIcon("/sitecore/shell/themes/standard/Network/16x16/earth2.png");
  tbuilder.SetBaseIDs("");

  // Create a Data section
  TemplateSection.Builder sbuilder = tbuilder.AddSection("Data",sectionID);
  sbuilder.SetIcon("Network/16x16/earth2.png");

  // Create a Title field
  TemplateField.Builder fbuilder = sbuilder.AddField("Title",fieldID);
  fbuilder.SetIcon("");
  fbuilder.SetShared(false);
  fbuilder.SetSortorder(0);
  fbuilder.SetStyle("");
  fbuilder.SetType("text");
  fbuilder.SetUnversioned(false);

  templates[0] = tbuilder.Template;
  tcollection.Reset(templates);

  // Return the template collection which contains our template
  return tcollection;

}


Prev Next